package com.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.server.pojo.Products;
import com.server.service.impl.ProductsServiceImpl;
import com.server.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 商城商品信息表 前端控制器
 * </p>
 *
 * @author ckick
 * @since 2025-09-20 09:29:27
 */
@RestController
public class ProductsController {
    @Autowired
    ProductsServiceImpl productsService;
    @RequestMapping("productsPage")
    ResponseMsg productsPage(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "5") Integer limit) {
        System.out.println("分页查询调用了");
        QueryWrapper<Products> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("product_id");
        IPage<Products> productsIPage = productsService.page(new Page<>(page, limit), wrapper);
        return ResponseMsg.getInstance(200, productsIPage);
    }

    @RequestMapping("addProduct")
    ResponseMsg addProduct(@RequestBody Products products) {
        return productsService.save(products) ? ResponseMsg.success() : ResponseMsg.getInstance(ResponseMsg.ERROR, "添加失败");
    }

    @RequestMapping("updateProduct")
    ResponseMsg updateProduct(@RequestBody Products products) {
        return productsService.updateById(products) ? ResponseMsg.success() : ResponseMsg.getInstance(ResponseMsg.ERROR, "修改失败");
    }

    @RequestMapping("deleteProduct")
    ResponseMsg deleteProduct(@RequestParam Integer productId) {
        return productsService.removeById(productId) ? ResponseMsg.success() : ResponseMsg.getInstance(ResponseMsg.ERROR, "删除失败");
    }
}
